package com.example.ceramics.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.ceramics.R;
import com.example.ceramics.adapter.OrderAdapter;
import com.example.ceramics.data.Cart;
import com.example.ceramics.data.Order;
import com.example.ceramics.database.dao.IOrderDao;
import com.example.ceramics.database.dao.OrderDaoImpl;
import com.example.ceramics.entity.GoodsEntity;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.io.Serializable;

public class OrderActivity extends BaseActivity {

    private OrderAdapter orderAdapter;
    private RecyclerView recyclerView;
    private ImageView ivReturn;
    private RefreshLayout refreshLayout;

    private IOrderDao orderDao;

    @Override
    protected int initLayout() {
        return R.layout.activity_order;
    }

    @Override
    protected void initView() {
        recyclerView = findViewById(R.id.recyclerView);
        ivReturn = findViewById(R.id.iv_return);
        refreshLayout = findViewById(R.id.refreshLayout);
    }

    @Override
    protected void initData() {
        orderDao = new OrderDaoImpl(mContext);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                switch_layout();
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                refreshLayout.finishLoadMore(1000);
                showToast("已经到底了！");
            }
        });

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);

        orderAdapter = new OrderAdapter(this);
        recyclerView.setAdapter(orderAdapter);
        orderAdapter.setOnItemClickListener(new OrderAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(Serializable obj) {
                GoodsEntity goodsEntity = (GoodsEntity) obj;
                navigateTo(DetailActivity.class, "detail", goodsEntity);
            }
        });

        getOrderList();

        ivReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void getOrderList() {
//        orderAdapter.setDatas(Order.getGoodsEntities());
        orderAdapter.setDatas(orderDao.getOrderList(getStringFromSp("account")));
        orderAdapter.notifyDataSetChanged();
    }

    public void switch_layout() {
        orderAdapter.setDatas(orderDao.getOrderList(getStringFromSp("account")));
//        orderAdapter.setDatas(Cart.getGoodsEntities());
        orderAdapter.notifyDataSetChanged();
        refreshLayout.finishRefresh(1000);
        showToast("刷新成功！");
    }
}